package com.dianwoba.pt.goodjob.schedule.intern;

import com.dianwoba.pt.goodjob.common.utils.JsonMapper;
import com.dianwoba.pt.goodjob.schedule.util.SpringContextHolder;
import com.dianwoba.pt.goodjob.schedule.util.ThreadUtil;
import org.quartz.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author 杨斌冰-工具组-技术中心
 *         <p>
 *         2017/6/9 17:40
 */
public abstract class AbstractQuartzJob implements Job {

    protected static JsonMapper jsonMapper = JsonMapper.getNonNullBinder();
    protected static ScheduleExecute scheduleExecute;

    protected final Logger logger     = LoggerFactory.getLogger(getClass());

    static {
        scheduleExecute = SpringContextHolder.getBean("scheduleExecute");
    }

    public void jobExecute(final Map<String, Object> contextMap) {

        long startTime = System.currentTimeMillis();
        logger.info("===========execute========================= contextMap:{}",
                jsonMapper.toJson(contextMap));
        try {
            scheduleExecute.rpcExecute(contextMap);
//                    //todo 统计
//                    CoeusMonitor.addNormal(System.currentTimeMillis() - startTime);
        } catch (Throwable e) {
            logger.error("execute error:{}, contextMap:{}", e, jsonMapper.toJson(contextMap));
//                    //todo 告警
//                    CoeusMonitor.addException("Job execute error", e, jsonMapper.toJson(contextMap));
        }

        /*ThreadUtil.schedThreadpool.submit(new Runnable() {
            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                logger.info("===========execute========================= contextMap:{}",
                        jsonMapper.toJson(contextMap));
                try {
                    scheduleExecute.rpcExecute(contextMap);
//                    //todo 统计
//                    CoeusMonitor.addNormal(System.currentTimeMillis() - startTime);
                } catch (Throwable e) {
                    logger.error("execute error:{}, contextMap:{}", e, jsonMapper.toJson(contextMap));
//                    //todo 告警
//                    CoeusMonitor.addException("Job execute error", e, jsonMapper.toJson(contextMap));
                }
            }
        });*/

    }


}
